package com.dtream.nacos.controller;

import com.dtream.nacos.entity.ExampleEntity;
import com.dtream.nacos.openfeig.ExampleClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/example")
public class ExampleController {
    @Autowired
    private ExampleClient exampleClient;

    /**
     * 获取所有用户数据
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public ResponseEntity<List<ExampleEntity>> getClientUsersList() {
        return exampleClient.getClientUsersList();
    }

    /**
     * 根据id获取用户数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResponseEntity<ExampleEntity> getClientUsers(@PathVariable String id) {
        return exampleClient.getClientUsers(id);
    }

    /**
     * 创建用户
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity<ExampleEntity> createUser(@Valid @RequestBody ExampleEntity users) {
        return exampleClient.createUser(users);
    }

    /**
     * 根据id 更新用户
     *
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResponseEntity<ExampleEntity> updateUser(@PathVariable("id") String id, @Valid @RequestBody ExampleEntity user) {
        return exampleClient.updateUser(id, user);
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> deleteUser(@PathVariable String id) {
        return exampleClient.deleteUser(id);
    }
}
